<form class="form-horizontal" role='form' id='frm'
	onsubmit='return push();'>

	<div class="control-group">
		<label class="control-label" for="file">上传</label>

		<div class="controls">
			<input type="file" id="file" name='file' class='input-large'
				accept="image/jpeg, image/png" style="display: none;" multiple />
		</div>

		<div class="controls">
			<img id='fileImg' style="width: 200px; height: 200px;" src="" />
		</div>
	</div>

	<div class="control-group">
		<label class="control-label"></label>

		<div class="controls">
			<input type="text" class='input-large' id="frm_img_1" name='img_1'
				placeholder="路径" />
		</div>
	</div>
</form>

<script>
	$(function() {
		$('#frm_img_1').click(function() {
			var _x = document.getElementById("frm_img_1");
			_x.select();
			document.execCommand("Copy");
			alert("内容已复制到剪切板");
		});
	});

	$(function() {
		$('#fileImg').click(function() {
			$('#file').click();
		});

		// 选择文件后触发事件
		$('#file').change(function() {
			if (!$('#file').val())
				return;
			upload();
			$('#file').val('');
		});
	});

	/**
	 *
	 * @return
	 */
	function upload() {
		var _data = new FormData();
		_data.append("file", $("#file")[0].files[0]);

		$.ajax({
			url : "./upload",
			type : "POST",
			dataType : 'JSON',
			data : _data,
			async : false,
			cache : false,
			contentType : false,
			processData : false,
			success : function(data) {
				if (!data.success) {
					return alert(data.msg);
				}

				$('#frm_img_1').val(data.data);
				$('#fileImg').attr(
						'src',
						'../../upload/' + data.data + '?ts='
								+ (new Date()).valueOf());
				alert('上传成功');
			}
		});
	}
</script>
